package com.tedu.weibo.interceptor;

import com.tedu.weibo.pojo.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*
        * HttpServletRequest请求对象中   提供了一个方法:
        * getSession(),该方法可以获取到对应当前客户端的会话对象
        * */
        HttpSession session = request.getSession();
        //通过Session获取用户的登录信息
        User user = (User) session.getAttribute("user");
        if (user == null){//没有登录
            //设置响应对象,要求浏览器请求"/index.html"页面内容
            response.sendRedirect("/index.html");
            return false;//拦截请求
        }else {
            return true;//放行
        }

    }

    /*@Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }*/
}
